package Dao.Daoimpl;

import Dao.UserLogin;
import Pojo.Judge;
import Pojo.Userlogin;
import utils.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserLoginimpl implements UserLogin {

    @Override
    /**
     * 从数据库获取信息比对登录账号密码是否正确
     * 返回的结果类型为自己写的judge
     *
     * 如果 登录输入的账号密码正确 并且是管理员返回的结果为true+1  反之 返回true + 0；
     *
     * 如果 登录输入的账号密码不正确 则返回 false + -1；
     *
     */
    public Judge judgeuser(Userlogin ul)  {
        Judge jd =new Judge(false,-1);
        boolean y;
        int anInt;
        Connection conn = null;
        try {
            conn = jdbc.getconnection();
            String sql ="select state from web_user where username=? and password=?";
            PreparedStatement preparedstatement = null;
            preparedstatement= conn.prepareStatement(sql);
            preparedstatement.setString(1,ul.getUsername());
            preparedstatement.setString(2,ul.getPassword());
            ResultSet res = preparedstatement.executeQuery();
            while (res.next()) {
                y=true;
                anInt = res.getInt(1);
                jd.setResult(y);
                jd.setState(anInt);
            }
            jdbc.close(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

//        关闭连接



        return jd;

    }
//        测试
  /*  public static void main(String[] args) {
        UserLoginimpl userLoginimpl = new UserLoginimpl();
        Userlogin ul = new Userlogin("ruan","123");
        try {
            Judge judgeuser = userLoginimpl.judgeuser(ul);
            System.out.println( judgeuser.toString());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }*/
}
